<?php

class TagsController extends AppController {

    public $uses = array(
        'Tag',
        'SourceDocumentTag',
        'SourceDocument',
    );
    public $scaffold;
    public $components = array(
        'ExtendedScaffold.ScaffoldUtil' => array(
            'indexSetFields' => array(
                'id',
                'name',
            ),
            'viewSetFields' => array(
                '_extended' => array(
                    array(
                        'label' => 'Principal',
                        'lines' => array(
                            'name'
                        )
                    ),
                    array(
                        'label' => 'Source Documents',
                        'listAssociation' => 'SourceDocumentTag',
                        'lines' => array(
                            'SourceDocument.name'
                        )
                    )
                )
            )
        )
    );
    public $moduleActions = array(
        array(
            'url' => array('action' => 'source_documents_all'),
            'hasId' => true,
            'format' => 'Source Documents (All)'
        ),
        array(
            'url' => array('action' => 'source_documents_no_tagged'),
            'hasId' => true,
            'format' => 'Source Documents (No tagged)'
        )
    );
    
    public function getPaginatorUtilFilters() {
        return array(
            'Name' => array(
                'conditions' => "Tag.name like ? || '%'"
            ),
        );
    }


    public function source_documents_all($id) {
        $this->source_documents($id, false);
    }

    public function source_documents_no_tagged($id) {
        $this->source_documents($id, true);
    }

    private function source_documents($id, $onlyNoTagged) {
        $tag = $this->Tag->findById($id);

        if ($this->request->isPut()) {
            $success = true;
            if (!empty($this->request->data['SourceDocument'])) {
                foreach ($this->request->data['SourceDocument'] as $sourceDocument) {
                    if (!$this->SourceDocumentTag->setEnabled(
                                    $sourceDocument['id']
                                    , $this->request->data['Tag']['id']
                                    , $sourceDocument['enabled']
                    )) {
                        $success = false;
                    }
                }
            }


            if ($success) {
                $this->flash('Source documents saved for tag.', '/tags/view/' . $tag['Tag']['id']);
            } else {
                $this->Session->setFlash('Failed to save source documents for tag.');
            }
        }

        $fields = array();
        $fields['Tag.id'] = array(
            'type' => 'hidden'
        );
        $fields['Tag.name'] = array(
            'readonly' => true,
            'label' => 'Tag name'
        );

        $this->request->data = array_merge($this->request->data, $tag);
        $k = 0;
        foreach ($this->SourceDocument->find('all') as $sourceDocument) {
            if (!$onlyNoTagged || !$sourceDocument['SourceDocument']['tagged']) {
                $fields["SourceDocument.$k.id"] = array(
                    'type' => 'hidden'
                );
                $fields["SourceDocument.$k.enabled"] = array(
                    'type' => 'checkbox',
                    'label' => $sourceDocument['SourceDocument']['name']
                );

                $this->request->data['SourceDocument'][$k]['id'] = $sourceDocument['SourceDocument']['id'];

                if (!$this->request->isPost()) {
                    $sourceDocumentTag = $this->SourceDocumentTag->findBySourceDocumentAndTag(
                            $sourceDocument['SourceDocument']['id'], $tag['Tag']['id']
                    );

                    if ($sourceDocumentTag) {
                        $this->request->data['SourceDocument'][$k]['enabled'] = true;
                    }
                }

                $k++;
            }
        }

        $this->ScaffoldUtil->render('form', $fields);
    }

    public function afterScaffoldSave($method) {
        if (parent::afterScaffoldSave($method)) {
            if ($method == 'add') {
                $this->redirect(array('action' => 'source_documents_no_tagged', $this->Tag->id));
            }

            return true;
        } else {
            return false;
        }
    }

}
